import { MANAGE_JOBS } from '@/credit/plugins/constant'

const config = [
  {
    code: 'org',
    name: '机构管理',
    children: [
      {
        code: 'view',
        name: '查看',
        auth: {
          job: MANAGE_JOBS,
          org: '',
          level: '',
          scope: '',
        },
      },
    ],
  },
]

const parse = (_auth, _user, _userOrg) => {
  return true
}

const state = {
  permissions: {},
}

const getters = {
  permissions: state => state.permissions,
}

const actions = {
  setPermissions({ commit }, { user, org }) {
    const permissions = {}
    const rev = (cfg, parentCode = '') => {
      for (const c of cfg) {
        const { code, auth } = c
        const currCode = parentCode + code
        permissions[currCode] = parse(auth, user, org)
        if (c.children) {
          rev(c.children, currCode + '.')
        }
      }
    }
    rev(config)

    commit('setPermissions', permissions)
  },
}

const mutations = {
  setPermissions: (state, payload) => (state.permissions = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
